package com.hnjz.sys.log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnjz.common.FyWebResult;
import com.hnjz.common.IPUtil;
import com.hnjz.common.PKUtil;
import com.hnjz.common.dao.domain.FyCounter;
import com.hnjz.common.security.CtxUtil;
import com.hnjz.common.util.LogUtil;

@Service("loggerManager")
public class LoggerManager {

	private static final Log log=LogFactory.getLog(LoggerManager.class);
	
	@Autowired
	private LoggerMapper loggerMapper;
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 根据Id获取日志
	 * @param id
	 * @return
	 */
	public Logger getLogger(String id){
		if(StringUtils.isBlank(id)){
			return null;
		}
		Logger logger=loggerMapper.findById(id);
		return logger;
	}
	
	/**
	 * 查询日志列表
	 * @param userName
	 * @param type
	 * @param url
	 * @param createtime
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public FyWebResult queryLogger(String userName,String type,String url,String createtime,String page,String pageSize){
		Logger logger=new Logger();
		logger.setUserName(userName);
		logger.setType(type);
		logger.setUrl(url);
		logger.setCreatetime(createtime);
		long count=this.loggerMapper.selectCountByPage(logger);
		FyCounter c=new FyCounter();
		c.setNum(count);
		c.setCurrentPage(Integer.parseInt(page));
		c.setPerPageNum(pageSize);
		c.execute();
		if(log.isDebugEnabled()){
			log.debug(c.getStartItem()+":"+c.getPerPageNum());
		}
		RowBounds rbs = new RowBounds(c.getStartItem(),c.getPerPageNum());
		FyWebResult fy = new FyWebResult(c);
		List<Logger> loggers=this.loggerMapper.findListByPage(logger, rbs);
		List<Map<String,String>> rows=new ArrayList<Map<String,String>>();
		Map<String,String> dataObject = null;
		for(Logger ele : loggers){
			dataObject=new HashMap<String,String>();
			dataObject.put("id", ele.getId());
			dataObject.put("userId", ele.getUserId());
			dataObject.put("userName", ele.getUserName());
			dataObject.put("type", ele.getType());
			dataObject.put("url", ele.getUrl());
			dataObject.put("createtime", ele.getCreatetime());
			dataObject.put("ipAddress", ele.getIpAddress());
			dataObject.put("macAddress", ele.getMacAddress());
			rows.add(dataObject);
		}
		fy.setRows(rows);
		if(log.isDebugEnabled()){
			log.debug("查询结果："+LogUtil.m(fy));
		}
		return fy;
	}
	
	/**
	 * 保存日志信息
	 * @param url
	 * @param type
	 */
	public void saveLogger(String url,String type) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Logger logger = null;
		Date cur = new Date();
		logger=new Logger();
		logger.setId(PKUtil.uuid());
		logger.setUserId(CtxUtil.getCurUser().getId());
		logger.setUserName(CtxUtil.getCurUser().getName());
		logger.setType(type);
		logger.setUrl(url);
		logger.setIpAddress(IPUtil.getIpAddr(request));
		logger.setMacAddress("");
		logger.setCreatetime(sdf.format(cur));
		
		this.loggerMapper.save(logger);
	}
}
